//
//  MyCustomNavigationbar2.m
//  Fitness War
//
//  Created by zhu on 12-3-17.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "MyCustomNavigationbar2.h"


@implementation MyCustomNavigationbar2
@synthesize goldNumLabel;
@synthesize goldNumImg;
@synthesize progressLabel;
@synthesize backgroundImg;

-(void) dealloc
{
    [progressLabel release];
    [goldNumImg release];
    [goldNumLabel release];
    [backgroundImg release];
    [super dealloc];
    
}

-(id)init
{
    [super init];
    if(self)
    {
        
        self.frame = CGRectMake(0, 0, 320, 32);
        
        //背景图片
       backgroundImg = [[UIImageView alloc] 
                                      initWithFrame:CGRectMake(0, 0, 320, 34)];
        backgroundImg.image = [[UIImage imageNamed:@"nav_bar.png"]autorelease];
        [self addSubview:backgroundImg];
        
        //金钱
        UIImageView *goldImg = [[UIImageView alloc] 
                                initWithFrame:CGRectMake(10, 6, 16, 17)];
        goldImg.image = [[UIImage imageNamed:@"gold.png"]autorelease];
        [backgroundImg addSubview:goldImg];
        
        //金钱数字
        goldNumImg = [[UIImageView alloc] initWithFrame:CGRectMake(30, 8, 20, 15)];
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold1.png"]
                            autorelease];
        [backgroundImg addSubview:goldNumImg];
        
        //金钱数字标签
        goldNumLabel = [[UILabel alloc] initWithFrame:CGRectMake(37, 10, 7, 9)];
        goldNumLabel.text = @"0";
        goldNumLabel.textColor =[UIColor whiteColor];
        goldNumLabel.font = [[UIFont systemFontOfSize:12]autorelease];
        goldNumLabel.backgroundColor = [UIColor clearColor];
        [backgroundImg addSubview:goldNumLabel];
        
        //进度条的背景图
        UIImageView *proBackground = [[UIImageView alloc] initWithFrame:    
                                      CGRectMake(160, 7, 100, 10)];
        proBackground.image =[[UIImage imageNamed:@"progOut.png"] 
                              autorelease];
        
        [backgroundImg addSubview:proBackground];
        
        //进度条label
        progressLabel = [[UILabel alloc] init];
        progressLabel = [[UILabel alloc] initWithFrame:CGRectMake(160, 19, 100, 10)];
        progressLabel.text = @"";
        progressLabel.textColor =[UIColor orangeColor];
        progressLabel.font = [[UIFont systemFontOfSize:12]autorelease];
        progressLabel.backgroundColor = [UIColor clearColor];
        [backgroundImg addSubview:progressLabel];
        
        [goldImg release];

    }
    return self;
}

  //设置进度条的值
- (void)setProgressVable:(int) _value
{

    
    if( _value == 0 ) 
    {
        //显示经验
        NSString *displayString = [NSString stringWithFormat:@"当前经验为:%d%%",_value];
        progressLabel.text = displayString;
        [displayString release];
        return;
    }
    else if(_value == 100)
    {
        //显示经验
        NSString *displayString = [NSString stringWithFormat:@"当前经验为:%d%%",_value];
        progressLabel.text = displayString;
        [displayString release];
        
        UIImage *img = [UIImage imageNamed:@"progIn.png"];
        UIImageView *proIn = [[UIImageView alloc] initWithFrame:    
                              CGRectMake(160, 7, 100, 10)];
        proIn.image = img;
        [backgroundImg addSubview:proIn];
        [img release];
        [proIn release];

        
    }
    else if(_value == 1)
    {
        UIImage *img = [UIImage imageNamed:@"prog_left.png"];
        UIImageView *proIn = [[UIImageView alloc] initWithFrame:    
                              CGRectMake(160, 7, 5, 10)];
        proIn.image =img;
        [backgroundImg addSubview:proIn];
        [img release];
        [proIn release];
    
    }
    else if ((_value > 1) && (_value < 100))
    {
        
        //显示经验      
        NSString *displayString = [NSString stringWithFormat:@"当前经验为:%d%%",_value];
        progressLabel.text = displayString;
        [displayString release];
        
        
        UIImage *img = [UIImage imageNamed:@"prog_left.png"];
        UIImageView *proIn = [[UIImageView alloc] initWithFrame:    
                              CGRectMake(160, 7, 5, 10)];
        proIn.image =img;
        [backgroundImg addSubview:proIn];

        if (_value >5)
        {       
            UIImage *anotherImg = [UIImage imageNamed:@"prog_red.png"];
            UIImageView *anotherProIn = [[UIImageView alloc] initWithFrame:    
                                  CGRectMake(165, 8.3, 93, 7)];
            anotherProIn.image =anotherImg;
            [backgroundImg addSubview:anotherProIn];
            [anotherImg release];
            [anotherProIn release];
            
            if(_value < 99)
            {
                _value -= 6;
                
                UIImage *blackImg = [UIImage imageNamed:@"prog_black.png"];
                UIImageView *blackProIn = [[UIImageView alloc] 
                initWithFrame:CGRectMake(165 +_value, 8.3, 93-_value, 7)];
                
                blackProIn.image =blackImg;
                [backgroundImg addSubview:blackProIn];
                [blackImg release];
                [blackProIn release];
            }
            
            
        }
 
        [img release];
        [proIn release];

    }
    
}

//设置金钱随金钱数增加
- (void)setMoneyNum:(int) _value
{
    
    
    NSString *tempStr = [NSString stringWithFormat:@"%d",_value];
    //按金钱数分类处理
    if(_value < 10)
    {
        goldNumLabel.text = tempStr;
    }
    else if(_value <100)
    {
        
        goldNumImg.frame = CGRectMake(30, 8, 33, 15);
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold2.png"]
                            autorelease];
        goldNumLabel.frame = CGRectMake(37, 10, 17, 9);
        goldNumLabel.text = tempStr;        
    }
    else if(_value <1000)
    {
        
        goldNumImg.frame = CGRectMake(30, 8, 38, 15);
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold3.png"]
                            autorelease];
        goldNumLabel.frame = CGRectMake(40, 10, 21, 9);
        goldNumLabel.text = tempStr;        
    }
    else if(_value <10000)
    {
        
        goldNumImg.frame = CGRectMake(30, 8, 43, 15);
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold4.png"]
                            autorelease];
        goldNumLabel.frame = CGRectMake(38.5, 10, 27, 9);
        goldNumLabel.text = tempStr;        
    }
    else if(_value <100000)
    {
        
        goldNumImg.frame = CGRectMake(30, 8,49, 15);
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold5.png"]
                            autorelease];
        goldNumLabel.frame = CGRectMake(38, 10, 35, 9);
        goldNumLabel.text = tempStr;        
    }
    else if(_value <1000000)
    {
        
        goldNumImg.frame = CGRectMake(30, 8,54, 15);
        goldNumImg.image = [[UIImage imageNamed:@"nav_Gold6.png"]
                            autorelease];
        goldNumLabel.frame = CGRectMake(37, 10, 42, 9);
        goldNumLabel.text = tempStr;        
    }
    
    
    [tempStr release];
}



@end
